﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Async;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ArcGIS.Client.Async.Tests
{
    [TestClass]
    public class CodeplexSamplesTests : SilverlightTest
    {
        [TestMethod, Asynchronous]
        public async void QuerySample()
        {
            var queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");
            var query = new Query { Text = "New", OutFields = { "*" }};
            var result = await queryTask.ExecuteTaskAsync(query);

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.FeatureSet);
            Assert.IsTrue(result.FeatureSet.Any());
            TestComplete();
        }

        [TestMethod, Asynchronous]
        public async void QueryWithBufferSample()
        {
            var sref = new SpatialReference(102100);

            // Creating a buffer
            var geometryService = new GeometryService("http://serverapps101.esri.com/arcgis/rest/services/Geometry/GeometryServer");
            var bufferParams = new BufferParameters
            {
                BufferSpatialReference = sref, // TODO: Please use more appropriate SR
                OutSpatialReference = sref,
                Unit = LinearUnit.Meter,
                Distances = { 100 },
                Features = { new Graphic { Geometry = new MapPoint(-9270000, 5247200, sref) } }
            };
            var bufferResult = await geometryService.BufferTaskAsync(bufferParams);
            var buffer = bufferResult.Results.Single();

            // Querying a layer
            var queryTask = new QueryTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer/2");
            var query = new Query
            {
                ReturnGeometry = true,
                OutSpatialReference = sref,
                Geometry = buffer.Geometry,
                OutFields = { "*" }
            };
            var queryResult = await queryTask.ExecuteTaskAsync(query);
            var features = queryResult.FeatureSet;

            Assert.IsNotNull(features);
            Assert.IsTrue(features.Any());
            TestComplete();
        }

        [TestMethod, Asynchronous]
        public async void ConcurrentQueriesSample()
        {
            // Creating queries
            var query = new Query
            {
                ReturnGeometry = true,
                OutSpatialReference = new SpatialReference(102100),
                Geometry = new MapPoint(-9270000, 5247200, new SpatialReference(102100)),
                OutFields = { "*" }
            };

            var queryTask1 = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");
            var queryTask2 = new QueryTask("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer/2");

            // Executing queries and adding tasks to a list
            var queries = new List<Task<QueryTaskResult>>
            {
                queryTask1.ExecuteTaskAsync(query),
                queryTask2.ExecuteTaskAsync(query)
            };

            // Waiting for all tasks to complete
            await TaskEx.WhenAll(queries);

            // Looping through query results
            foreach (var task in queries)
            {
                var result = task.Result;
                Assert.IsNotNull(result);
                Assert.IsNotNull(result.FeatureSet);
                Assert.IsTrue(result.FeatureSet.Any());
            }
            TestComplete();
        }
    }
}
